Skip to content

Method: ofName(String, Function)

1: /*
2: * *********************************************************************************************************************
3: *
4: * blueMarine II: Semantic Media Centre
5: * http://tidalwave.it/projects/bluemarine2
6: *
7: * Copyright (C) 2015 - 2021 by Tidalwave s.a.s. (http://tidalwave.it)
8: *
9: * *********************************************************************************************************************
10: *
11: * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
12: * the License. You may obtain a copy of the License at
13: *
14: * http://www.apache.org/licenses/LICENSE-2.0
15: *
16: * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
17: * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations under the License.
19: *
20: * *********************************************************************************************************************
21: *
22: * git clone https://bitbucket.org/tidalwave/bluemarine2-src
23: * git clone https://github.com/tidalwave-it/bluemarine2-src
24: *
25: * *********************************************************************************************************************
26: */
27: package it.tidalwave.bluemarine2.commons.test;
28:
29: import javax.annotation.Nonnull;
30: import java.util.Collection;
31: import java.util.function.Function;
32: import java.util.stream.Collector;
33: import java.util.stream.Stream;
34: import java.io.IOException;
35: import java.nio.file.Files;
36: import java.nio.file.Path;
37: import lombok.AllArgsConstructor;
38: import lombok.Getter;
39: import lombok.extern.slf4j.Slf4j;
40: import static java.util.stream.Collectors.*;
41: import static java.nio.file.FileVisitOption.FOLLOW_LINKS;
42: import static lombok.AccessLevel.PRIVATE;
43:
44: /***********************************************************************************************************************
45: *
46: * This class represents a file from a test set. It contains the test set name and the file path.
47: *
48: * @author Fabrizio Giudici
49: *
50: **********************************************************************************************************************/
51: @AllArgsConstructor(access = PRIVATE) @Getter @Slf4j
52: public class TestSetTriple
53: {
54: public final String testSetName;
55:
56: public final Path testSetPath;
57:
58: public final Path filePath;
59:
60: /*******************************************************************************************************************
61: *
62: * Creates a {@link Stream} containing the files in the provided test sets. Files are resolved using the
63: * {@link TestSetLocator}.
64: *
65: * @param testSetNames the test set names
66: * @return the {@code Stream}
67: *
68: ******************************************************************************************************************/
69: @Nonnull
70: public static Stream<TestSetTriple> streamOfTestSetTriples (@Nonnull final Collection<String> testSetNames)
71: {
72: return streamOfTestSetTriples(testSetNames, n -> TestSetLocator.getMusicTestSetsPath().resolve(n));
73: }
74:
75: /*******************************************************************************************************************
76: *
77: * Creates a {@link Stream} containing the files in the provided test sets.
78: *
79: * @param testSetNames the test set names
80: * @param basePathProvider a function that resolves a test set name to a test set base path
81: * @return the {@code Stream}
82: *
83: ******************************************************************************************************************/
84: @Nonnull
85: public static Stream<TestSetTriple> streamOfTestSetTriples (@Nonnull final Collection<String> testSetNames,
86: @Nonnull final Function<String, Path> basePathProvider)
87: {
88: return testSetNames.stream()
89: .map(testSetName -> TestSetTriple.ofName(testSetName, basePathProvider))
90: .flatMap(TestSetTriple::walk)
91: .filter(t -> Files.isRegularFile(t.filePath))
92: .filter(t -> !t.filePath.getFileName().toString().startsWith(".")); // isHidden() throws exception
93: }
94:
95: /*******************************************************************************************************************
96: *
97: * Returns a {@link Collector) that converts the stream into an {@code Object[][]) suitable for a
98: * TestNG {@code DataProvider}.
99: *
100: * @return the {@code Collector}
101: *
102: ******************************************************************************************************************/
103: @Nonnull
104: public static Collector<Object, ?, Object[][]> toTestNGDataProvider()
105: {
106: return collectingAndThen(mapping(object -> new Object[] { object }, toList()),
107: list -> list.toArray(new Object[0][0]));
108: }
109:
110: /*******************************************************************************************************************
111: *
112: * Returns the path of the test set file relativized to the root of the test set.
113: *
114: * @return the relative path
115: *
116: ******************************************************************************************************************/
117: @Nonnull
118: public Path getRelativePath()
119: {
120: return testSetPath.relativize(filePath);
121: }
122:
123: /*******************************************************************************************************************
124: *
125: * {@inheritDoc}
126: *
127: ******************************************************************************************************************/
128: @Override @Nonnull
129: public String toString()
130: {
131: return String.format("%s: %s : %s", testSetName, testSetPath, getRelativePath());
132: }
133:
134: /*******************************************************************************************************************
135: *
136: ******************************************************************************************************************/
137: @Nonnull
138: private static TestSetTriple ofName (@Nonnull final String testSetName,
139: @Nonnull final Function<String, Path> basePathProvider)
140: {
141: return new TestSetTriple(testSetName, basePathProvider.apply(testSetName), null);
142: }
143:
144: /*******************************************************************************************************************
145: *
146: ******************************************************************************************************************/
147: @Nonnull
148: private Stream<TestSetTriple> walk()
149: {
150: try
151: {
152: if (Files.exists(testSetPath))
153: {
154: return Files.walk(testSetPath, FOLLOW_LINKS)
155: .map(path -> new TestSetTriple(testSetName, testSetPath, path));
156: }
157: else
158: {
159: log.warn("MISSING TEST SET: {} - {}", testSetName, testSetPath);
160: return Stream.empty();
161: }
162: }
163: catch (IOException e)
164: {
165: throw new RuntimeException(e);
166: }
167: }
168: }
169: